package de.kalinasch.pa.placereminder;

import java.util.ArrayList;

import de.kalinasch.pa.placereminder.database.IncidentDataSource;
import de.kalinasch.pa.placereminder.dmo.IncidentDMO;
import de.kalinasch.pa.placereminder.util.InputValidator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class displays the data of an saved incident. The user can change or delete the displayed incident.
 * 
 * 
 * @author dkalinasch
 */
public class ShowReminderDetailActivity extends Activity implements OnClickListener {
	public static final String INCIDENT_ID = "incident_id";

	private int id = 0;
	private IncidentDMO loadIncident;
	private IncidentDataSource adapter;

	@Override
	public void onCreate(Bundle _saveInstanceState) {
		super.onCreate(_saveInstanceState);
		setContentView(R.layout.showreminderdetail);

		id = getIntent().getIntExtra(INCIDENT_ID, 0);
		Log.d(this.getLocalClassName(), "incident_id = " + id);
		adapter = new IncidentDataSource(this);
		loadIncident = new IncidentDMO();
		loadIncident = adapter.getIncident(id);

		TextView strasse = (TextView) findViewById(R.id.edit_street);
		strasse.setText(loadIncident.getStreet());

		TextView ort = (TextView) findViewById(R.id.edit_town);
		ort.setText(loadIncident.getTown());

		EditText titel = (EditText) findViewById(R.id.detail_edit_title);
		titel.setText(loadIncident.getTitle());

		EditText beschreibung = (EditText) findViewById(R.id.detail_edit_description);
		beschreibung.setText(loadIncident.getDescription());

		// Auswahlbox für Umkreis der Erinnerungsauslösung
		Spinner s1 = (Spinner) findViewById(R.id.radius);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.radien,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s1.setAdapter(adapter);
		s1.setSelection(getSpinnerValuePosition(adapter, loadIncident.getRadius()));

		Button delete = (Button) findViewById(R.id.detail_button_delete);
		delete.setOnClickListener(this);

		Button save = (Button) findViewById(R.id.detail_button_save);
		save.setOnClickListener(this);
	}

	/**
	 * 
	 * @param _adapter
	 *            the data of the spinner that the use can select
	 * @param _radius
	 *            the value that was saved in the database
	 * @return the position of the saved data in the spinner
	 */
	private int getSpinnerValuePosition(ArrayAdapter<CharSequence> _adapter, int _radius) {
		String radiusString = String.valueOf(_radius) + "m";
		return _adapter.getPosition(radiusString);
	}

	@Override
	public void onClick(View _button) {
		if ((_button.getId()) == R.id.detail_button_delete) {
			deleteIncidentFromDB();
			Intent intent = new Intent(_button.getContext(), ListReminderActivity.class);
			_button.getContext().startActivity(intent);
		}
		if (_button.getId() == R.id.detail_button_save) {
			updateIncidentDB();
			Intent intent = new Intent(this, ListReminderActivity.class);
			startActivity(intent);
		}
	}

	/**
	 * After the user changed the data of the displayed incident, the new value will be saved.
	 */
	private void updateIncidentDB() {
		IncidentDMO updateIncident = readIncidentData();
		if (updateIncident != null) {
			updateIncident.setId(loadIncident.getId());
			adapter.updateIncident(updateIncident);
		} else {
			Toast.makeText(this, "Reminder could not been updated", 3);
		}
	}

	/**
	 * Delete the displayed incident.
	 */
	private void deleteIncidentFromDB() {
		adapter.deleteIncident(id);
	}

	/**
	 * Reads the user input from the form to update the loaded incident.
	 * 
	 * @return IncidentDMO with the data specified by the user
	 */
	private IncidentDMO readIncidentData() {
		ArrayList<EditText> inputToValidate = new ArrayList<EditText>();
		EditText title = (EditText) findViewById(R.id.detail_edit_title);
		inputToValidate.add(title);
		EditText description = (EditText) findViewById(R.id.detail_edit_description);
		inputToValidate.add(description);

		if (!InputValidator.checkEditTextInput(inputToValidate)) {
			return null;
		}

		// Aus Umkreis-String int-Variable erzeugen
		Spinner radius = (Spinner) findViewById(R.id.radius);
		String umkreis = (String) radius.getSelectedItem();
		int laenge = umkreis.length();
		umkreis = umkreis.substring(0, (laenge - 1));
		int rad = Integer.parseInt(umkreis);

		return new IncidentDMO(loadIncident.getStreet(), loadIncident.getTown(), title.getText().toString(),
				description.getText().toString(), rad, loadIncident.getLatitude(), loadIncident.getLongitude());

	}
}
